package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.AuditoriumsMapper;
import com.zretc.entity.Auditoriums;
import com.zretc.service.AuditoriumsService;
import com.zretc.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AuditoriumsServiceImpl implements AuditoriumsService {
    @Autowired
    private AuditoriumsMapper auditoriumsMapper;
    @Override
    public boolean insert(Auditoriums auditoriums) {
        return auditoriumsMapper.insert(auditoriums)>0;
    }

    @Override
    public boolean delete(Integer auditoriumsId) {
        return auditoriumsMapper.delete(auditoriumsId)>0;
    }

    @Override
    public boolean update(Auditoriums auditoriums) {
        return auditoriumsMapper.update(auditoriums)>0;
    }

    @Override
    public PageInfo<Auditoriums> getAuditoriumsList(Map<String, String> condition) {
        String auditoriumsName = condition.get("auditoriumsName");
        if(auditoriumsName==null||"".equals(auditoriumsName)){
            condition.put("auditoriumsName",null);
        }else{
            condition.put("auditoriumsName","%"+auditoriumsName+"%");
        }
        // 获取页码
        String num = condition.get("pageNum");
        if (num == null){
            num = "1";
        }
        Integer pageNum = Integer.valueOf(num);
        // 获取显示数量
        String size = condition.get("pageSize");
        if (size == null){
            size = "10";
        }
        Integer pageSize = Integer.valueOf(size);
        PageHelper.startPage(pageNum,pageSize);
        List<Auditoriums> list = auditoriumsMapper.getAuditoriumsList(condition);
        PageInfo<Auditoriums> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Auditoriums getAuditoriumsById(Integer auditoriumsId) {
        return auditoriumsMapper.getAuditoriumsById(auditoriumsId);
    }

    @Override
    @Transactional
    public boolean deleteAuditoriumsList(List<String> auditoriumsList) {
        try{
            for (String string : auditoriumsList) {
                Integer auditoriumsId = Integer.valueOf(string);
                auditoriumsMapper.delete(auditoriumsId);
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }

    @Override
    public HSSFWorkbook export() {
        // 设置标题行
        Map<String,String> head = new LinkedHashMap<>();
        head.put("auditoriumsId","影厅编号");
        head.put("auditoriumsName","影厅名称");
        // 填充数据
        List<Auditoriums> list = auditoriumsMapper.getAuditoriumsList(new HashMap<>());
        HSSFWorkbook hssfWorkbook = ExcelUtil.creatExcel(head,list);
        return hssfWorkbook;
    }

    @Override
    @Transactional
    public boolean importExcel(List<Object> list) {
        try {
            for (Object object : list) {
                Auditoriums auditoriums = (Auditoriums)object;
                auditoriumsMapper.insert(auditoriums);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    @Override
    public HSSFWorkbook auditoriumsDemo() {
        // 设置标题行
        Map<String,String> head = new LinkedHashMap<>();
        head.put("auditoriumsName","auditoriumsName");
        // 填充数据
        HSSFWorkbook hssfWorkbook = ExcelUtil.creatExcel(head,new ArrayList());
        return hssfWorkbook;
    }


}
